package com.yiwei.zhh.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yiwei.zhh.R;
import com.yiwei.zhh.api.Api;
import com.yiwei.zhh.event.XueXinQuestionEvent;
import com.yiwei.zhh.model.MiBaoQuestionModel;
import com.yiwei.zhh.util.Network;
import com.yiwei.zhh.util.RxLifeUtil;
import com.yiwei.zhh.util.ToastUtil;
import com.yiwei.zhh.util.UI;
import com.yiwei.zhh.view.MiBaoQuestionDialog;
import com.yiwei.zhh.view.V19FrameLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class V3XueXinQuestionSetActivity extends BaseActivity implements MiBaoQuestionDialog.OnSureListener {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_question_one)
    TextView txtQuestionOne;
    @BindView(R.id.btn_question_one)
    FrameLayout btnQuestionOne;
    @BindView(R.id.edit_answer_one)
    EditText editAnswerOne;
    @BindView(R.id.txt_question_two)
    TextView txtQuestionTwo;
    @BindView(R.id.btn_question_two)
    FrameLayout btnQuestionTwo;
    @BindView(R.id.edit_answer_two)
    EditText editAnswerTwo;
    @BindView(R.id.txt_question_three)
    TextView txtQuestionThree;
    @BindView(R.id.btn_question_three)
    FrameLayout btnQuestionThree;
    @BindView(R.id.edit_answer_three)
    EditText editAnswerThree;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    private Unbinder unbinder;
    private MiBaoQuestionDialog miBaoQuestionDialog;
    private List<MiBaoQuestionModel.DataBean> dataBeanList;
    private int seletPoi = 0;
    private String keyOne;
    private String keyTwo;
    private String keyThree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xuexin_question_set_v3);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("学信网注册");
        miBaoQuestionDialog = new MiBaoQuestionDialog(this).setOnSelect(this);
        getXueXinQuestions();
    }

    @OnTextChanged({R.id.edit_answer_one, R.id.edit_answer_two, R.id.edit_answer_three})
    void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(keyOne) && !TextUtils.isEmpty(keyTwo) && !TextUtils.isEmpty(keyThree) && !TextUtils.isEmpty(UI.toString(editAnswerOne)) && !TextUtils.isEmpty(UI.toString(editAnswerTwo)) && !TextUtils.isEmpty(UI.toString(editAnswerThree))) {
            btnSubmit.setSelected(true);
        } else {
            btnSubmit.setSelected(false);
        }
    }

    public void getXueXinQuestions() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();

        Api.api_service.getXueXinQuestions().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(model -> {
                    setQuestion(model.data);
                });
    }

    private void setQuestion(List<MiBaoQuestionModel.DataBean> data) {
        dataBeanList = data;
        miBaoQuestionDialog.setData(data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    @Override
    public void onSure(String id, String name) {
        switch (seletPoi) {
            case 0:
                keyOne = id;
                txtQuestionOne.setText(name);
                break;
            case 1:
                keyTwo = id;
                txtQuestionTwo.setText(name);
                break;
            case 2:
                keyThree = id;
                txtQuestionThree.setText(name);
                break;
        }
    }

    private boolean codeViewInputCheck() {
        if (TextUtils.isEmpty(keyOne)) {
            ToastUtil.makeText(getApplicationContext(), "第一个问题不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(keyTwo)) {
            ToastUtil.makeText(getApplicationContext(), "第二个问题不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(keyThree)) {
            ToastUtil.makeText(getApplicationContext(), "第三个问题不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(UI.toString(editAnswerOne))) {
            ToastUtil.makeText(getApplicationContext(), "第一个答案不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(UI.toString(editAnswerTwo))) {
            ToastUtil.makeText(getApplicationContext(), "第二个答案不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(UI.toString(editAnswerThree))) {
            ToastUtil.makeText(getApplicationContext(), "第三个答案不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

    @OnClick({R.id.btn_back, R.id.btn_submit,R.id.btn_question_one, R.id.btn_question_two, R.id.btn_question_three})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_question_one:
                seletPoi = 0;
                if (dataBeanList == null) {
                    getXueXinQuestions();
                } else {
                    miBaoQuestionDialog.show();
                }
                break;
            case R.id.btn_question_two:
                seletPoi = 1;
                if (dataBeanList == null) {
                    getXueXinQuestions();
                } else {
                    miBaoQuestionDialog.show();
                }
                break;
            case R.id.btn_question_three:
                seletPoi = 2;
                if (dataBeanList == null) {
                    getXueXinQuestions();
                } else {
                    miBaoQuestionDialog.show();
                }
                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_submit:
                if (codeViewInputCheck()) {
                    EventBus.getDefault().post(new XueXinQuestionEvent(keyOne, keyTwo, keyThree,
                            UI.toString(editAnswerOne), UI.toString(editAnswerTwo), UI.toString(editAnswerThree)));
                    finish();
                }

                break;
        }
    }
}
